package com.yxdj.user.controller;

import com.yxdj.common.core.domain.R;
import com.yxdj.common.core.utils.poi.ExcelUtil;
import com.yxdj.common.core.web.controller.BaseController;
import com.yxdj.common.core.web.domain.AjaxResult;
import com.yxdj.common.core.web.page.TableDataInfo;
import com.yxdj.common.log.annotation.Log;
import com.yxdj.common.log.enums.BusinessType;
import com.yxdj.common.security.annotation.InnerAuth;
import com.yxdj.common.security.annotation.RequiresPermissions;
import com.yxdj.user.api.domain.Address;
import com.yxdj.user.service.IAddressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户地址信息Controller
 *
 * @author mty
 * @date 2025-03-18
 */
@Tag(name = "用户地址信息管理")
@RestController
@RequestMapping("/address")
public class AddressController extends BaseController
{
    @Autowired
    private IAddressService addressService;

    /**
     * 查询用户地址信息列表
     */
    @Operation(summary = "查询用户地址信息列表")
    @RequiresPermissions("user:address:list")
    @GetMapping("/list")
    public TableDataInfo list(@Parameter(name = "Address" ,description = "Address信息") Address address)
    {
        startPage();
        List<Address> list = addressService.selectAddressList(address);
        return getDataTable(list);
    }

    /**
     * 导出用户地址信息列表
     */
    @Operation(summary = "导出用户地址信息列表")
    @RequiresPermissions("user:address:export")
    @Log(title = "用户地址信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Parameter(name = "response",required = true, description = "response响应对象") HttpServletResponse response, @Parameter(name = "address" ,required = true, description = "Address信息") Address address)
    {
        List<Address> list = addressService.selectAddressList(address);
        ExcelUtil<Address> util = new ExcelUtil<Address>(Address.class);
        util.exportExcel(response, list, "用户地址信息数据");
    }

    /**
     * 获取用户地址信息详细信息
     */
    @Operation(summary = "获取用户地址信息详细信息")
    @RequiresPermissions("user:address:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@Parameter(name = "id" ,required = true, description = "id信息") @PathVariable("id") Long id)
    {
        return success(addressService.selectAddressById(id));
    }

    /**
     * 新增用户地址信息
     */
    @Operation(summary = "新增用户地址信息")
    @RequiresPermissions("user:address:add")
    @Log(title = "用户地址信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Parameter(name = "address",required = true, description = "Address信息") @RequestBody Address address)
    {
        return toAjax(addressService.insertAddress(address));
    }

    /**
     * 修改用户地址信息
     */
    @Operation(summary = "修改用户地址信息")
    @RequiresPermissions("user:address:edit")
    @Log(title = "用户地址信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Parameter(name = "address",required = true, description = "Address信息") @RequestBody Address address)
    {
        return toAjax(addressService.updateAddress(address));
    }

    /**
     * 删除用户地址信息
     */
    @Operation(summary = "删除用户地址信息")
    @RequiresPermissions("user:address:remove")
    @Log(title = "用户地址信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@Parameter(name = "ids" ,required = true, description = "主键串") @PathVariable Long[] ids)
    {
        return toAjax(addressService.deleteAddressByIds(ids));
    }

    @InnerAuth
    @Operation(summary = "供微服务内部远程调用根据用户id获取用户所有地址信息")
    @GetMapping("/app/getUserAddressByUserId/{userId}")
    public R<List<Address>> getUserAddressByUserId(@PathVariable(name = "userId") Long userId){
        List<Address> addressList = addressService.getUserAddressByUserId(userId);
        return R.ok(addressList);
    }

    @InnerAuth
    @Operation(summary = "供微服务内部远程调用根据id获取用户上门地址信息")
    @GetMapping("/app/getAddressById/{id}")
    public R<Address> getAddressById(@PathVariable(name = "id") Long id){
        Address address = addressService.getAddressById(id);
        return R.ok(address);
    }
}
